//
//  KRReadComment.m
//  cookBook
//
//  Created by kuroken on 16/1/15.
//  Copyright © 2016年 kuroken. All rights reserved.
//

#import "KRReadComment.h"
#import <RegexKitLite.h>
#import <MJExtension.h>
#import "KRConst.h"
#import "KREmotionTool.h"
#import "KREmotion.h"
#import "KRTextPart.h"
#import "__lldb_autogen_nspair.h"

@implementation KRReadComment
+ (void)load
{
    [KRReadComment mj_setupReplacedKeyFromPropertyName:^__lldb_autogen_nspair *{
        return (__lldb_autogen_nspair *)@{                                          
                                          @"comment_userID" : @"UserId",
                                          @"comment_avatar" : @"Avatar",
                                          @"comment_atUserID" : @"AtUserId",
                                          @"comment_atUserName" : @"AtUserName",
                                          @"comment_atContent" : @"AtContent",
                                          @"comment_content" : @"Content",
                                          @"comment_userName" : @"UserName",
                                          @"comment_createTime" : @"CreateTime",
                                          @"comment_vip" : @"IsVip"
                                          };
    }];
}

- (NSMutableAttributedString *)attributedTextWithText:(NSString *)text
{
    NSMutableAttributedString *attributedText = [[NSMutableAttributedString alloc] init];
    
    // 表情的规则
    NSString *emotionPattern = @"\\[[\\u4e00-\\u9fa5]+\\]";
    
    NSMutableArray *parts = [NSMutableArray array];
    
    // 遍历所有的特殊字符串
    [text enumerateStringsMatchedByRegex:emotionPattern usingBlock:^(NSInteger captureCount, NSString *const __unsafe_unretained *capturedStrings, const NSRange *capturedRanges, volatile BOOL *const stop) {
//        KRLog(@"%d %@ %@", (int)captureCount, *capturedStrings, NSStringFromRange(*capturedRanges));
        if ((*capturedRanges).length == 0) return;
        
        KRTextPart *part = [[KRTextPart alloc] init];
        part.special = YES;
        part.text = *capturedStrings;
        part.emotion = [part.text hasPrefix:@"["] && [part.text hasSuffix:@"]"];
        part.range = *capturedRanges;
        [parts addObject:part];
    }];

    // 遍历所有非特殊字符
    [text enumerateStringsSeparatedByRegex:emotionPattern usingBlock:^(NSInteger captureCount, NSString *const __unsafe_unretained *capturedStrings, const NSRange *capturedRanges, volatile BOOL *const stop) {
        if ((*capturedRanges).length == 0) return;
        
        KRTextPart *part = [[KRTextPart alloc] init];
        part.text = *capturedStrings;
        part.range = *capturedRanges;
        [parts addObject:part];
    }];

    // 从小到大排序
    [parts sortUsingComparator:^NSComparisonResult(KRTextPart *part1, KRTextPart *part2) {
        if (part1.range.location > part2.range.location) {
            return NSOrderedDescending;
        }
        return NSOrderedAscending;
    }];
    
    UIFont *font = [UIFont systemFontOfSize:17];
    for (KRTextPart *part in parts) {
        NSAttributedString *subStr = nil;
        if (part.isEmotion) {
            NSTextAttachment *attch = [[NSTextAttachment alloc] init];
            NSString *name = [KREmotionTool emotionsWithChs:part.text].png;
            if (name) { // 能找到对应的图片
                attch.image = [UIImage imageNamed:name];
                attch.bounds = CGRectMake(0, -3, font.lineHeight, font.lineHeight);
                subStr = [NSAttributedString attributedStringWithAttachment:attch];
            } else { // 表情图片不存在
                subStr = [[NSAttributedString alloc] initWithString:part.text];
            }
        } else {
            subStr = [[NSAttributedString alloc] initWithString:part.text];
        }
        [attributedText appendAttributedString:subStr];
    }
    
    // 一定要设置字体,保证计算出来的尺寸是正确的
    [attributedText addAttribute:NSFontAttributeName value:font range:NSMakeRange(0, attributedText.length)];
    
    return attributedText;
}

- (void)setComment_content:(NSString *)comment_content
{
    _comment_content = [comment_content copy];
    
    self.attributed_comment_content = [self attributedTextWithText:comment_content];
}

- (void)setComment_atContent:(NSString *)comment_atContent
{
    _comment_atContent = [comment_atContent copy];
    
    self.attributed_comment_atContent = [self attributedTextWithText:comment_atContent];}

@end
